package com.bio.service.user.impl;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang.StringUtils;

import com.bio.dao.user.UserDAO;
import com.bio.entity.user.User;
import com.bio.exception.OperationException;
import com.bio.exception.Result;
import com.bio.service.impl.AbstractService;
import com.bio.service.user.UserService;
import com.bio.utils.Pager;
import com.bio.utils.Pagination;
import com.bio.utils.md5.MyMD5Util;

public class UserServiceImpl extends AbstractService<User, UserDAO> implements
		UserService {

	public UserServiceImpl(UserDAO dao) {
		super(dao);
	}

	@Override
	public Result login(String email, String password, String receiveVerifyCode, String realVerifyCode) {
		Result result = new Result();
		if(StringUtils.isEmpty(receiveVerifyCode) || !receiveVerifyCode.equals(realVerifyCode)){
			return new Result(false, "验证码错误！");
		}
		try {
			User user = dao.getUserByEmail(email);
			if(null == user){
				return new Result(false, "该用户不存在！");
			}
			String passwordInDb = user.getPassword();
			boolean isValidPassword = MyMD5Util.validPassword(password, passwordInDb );
			if(!isValidPassword){
				return new Result(false, "密码不正确！");
			}
			result.setSuccess(true);
			result.setObject(user);
		} catch (NoSuchAlgorithmException e) {
			result.setSuccess(false);
			result.setMessage("加密算法错误!");
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			result.setSuccess(false);
			result.setMessage("不支持加密!");
		}
		
		return result;
	}

	@Override
	public Pager<User> findPagerByCompanyId(Pagination pagination,
			Long companyId) {
		return dao.findPagerByCompanyId(pagination, companyId);
	}

	@Override
	public User getUserByNameAndCompany(String name, Long companyId) {
		return dao.getUserByNameAndCompany(name, companyId);
	}

	@Override
	public Result accountValidate(User user){
		String userName = user.getUserName();
		String email = user.getEmail();
		Long id = user.getId();
		String repassword = user.getRepassword();
		int count1 = dao.countUsersByName(userName, id);
		int count2 = dao.countUsersByMail(email, id);
		if(count1 > 0){
			return new Result(false, "用户名已经存在!");
		}
		if(count2 > 0){
			return new Result(false, "邮箱已经存在!");
		}
		if(StringUtils.isNotEmpty(repassword) && !repassword.equals(user.getPassword())){
			return new Result(false, "两次密码不一致!");
		}
		return new Result(true, null);
	}

	@Override
	public User getUserByEmail(String email) {
		return dao.getUserByEmail(email);
	}

	@Override
	public User saveByMd5(User user) throws OperationException {
		String password = user.getPassword();
		try {
			//MD5加密
			password = MyMD5Util.getEncryptedPwd(password);
			user.setPassword(password);
			user = super.save(user);
		} catch (NoSuchAlgorithmException e) {
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public User get(Serializable id) {
		User user = super.get(id);
		if(null != user){
			byte[] bytes = MyMD5Util.hexStringToByte(user.getPassword());
			user.setPassword(MyMD5Util.byteToHexString(bytes));
		}
		return user;
	}
	
}
